<?php

namespace App\Console\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

class ModelMakeCommand extends Command
{

    protected function buildClass($name): string
    {
        if ($this->option('table')) {
            $classVar = $this->option('table');
        } else {
            $classVar = Str::snake(lcfirst(str_replace($this->getNamespace($name) . '\\', '', $name)));
        }

        $replace = [
            '{{classVariable}}' => $classVar,
            '{{ classVariable }}' => $classVar,
        ];

        $serviceNamespace = $this->getNamespace($name);
        $replace["use {$serviceNamespace}\Model;\n"] = '';

        return str_replace(
            array_keys($replace), array_values($replace), parent::buildClass($name)
        );
    }

    protected function resolveStubPath($stub): string
    {
        return file_exists($stub1 = __DIR__ . $stub)
            ? $stub1
            : parent::resolveStubPath($stub);
    }

    protected function getOptions(): array
    {
        return [...parent::getOptions(),
            ['table', 't', InputOption::VALUE_OPTIONAL, 'table .....'],
        ];
    }
}
